import 'package:cached_network_image/cached_network_image.dart';
import 'package:chewie/chewie.dart';
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/api/api.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/page_common/show_img_page.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/input_widget.dart';
import 'package:get/get.dart';

import 'guang_chang_details_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/11/25 15:11
/// filePath： lib/page/square/details/guang_chang_details_view.dart
/// desc: 广场帖子详情
/// <summary>
///
class GuangChangDetailsPage extends StatefulWidget {
  @override
  _GuangChangDetailsPageState createState() => _GuangChangDetailsPageState();
}

class _GuangChangDetailsPageState extends State<GuangChangDetailsPage> {
  final logic = Get.put(GuangChangDetailsLogic());
  FijkPlayer? player;

  getPlay() async{
    player = FijkPlayer();
    await player!.setDataSource(Api.testVideo1,showCover: true);
    setState(() { });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.postId = Get.arguments;
    logic.postDetail();
    logic.fileList.insert(logic.fileList.length, logic.selectAddBtn());
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GuangChangDetailsLogic>(
      builder: (controller) {
        return Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: appBarWidget(title: "动态详情",actions: [
            FlatButton(onPressed: (){
              Get.dialog(
                Scaffold(
                  appBar: appBarWidget(title: "投诉"),
                  backgroundColor: Colors.white,
                  body: GetBuilder<GuangChangDetailsLogic>(
                    builder: (controller) {
                      return Padding(
                        padding: EdgeInsets.all(15),
                        child: ListView(
                          padding: EdgeInsets.all(0),
                          children: [
                            Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    InkWidget.title("图片证据\n"),
                                    InkWidget.title("${logic.imgs.length}/9\n"),
                                  ],
                                ),
                                Container(
                                  width: Get.width,
                                  child: ListView(
                                    physics: NeverScrollableScrollPhysics(),
                                    padding: EdgeInsets.all(0),
                                    shrinkWrap: true,
                                    children: [
                                      Wrap(
                                        alignment: logic.fileList.length % 3 == 0 ?  WrapAlignment.spaceBetween: WrapAlignment.start,
                                        runSpacing: 15,
                                        spacing: 15,
                                        children: List.generate(logic.fileList.length, (index) {
                                          return logic.fileList[index];
                                        }),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                            InkWidget.title("\n投诉内容\n"),
                            Container(
                              padding: EdgeInsets.all(10),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(color: Colors.grey.shade200)
                              ),
                              height: 200,
                              child: TextField(
                                controller: logic.textEditingControllerJuBao,
                                keyboardType: TextInputType.multiline,
                                maxLines: 99999,
                                decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: "请输入投诉内容...",
                                  hintStyle: TextStyle(color: Colors.grey),
                                ),
                                maxLength: 100,
                              ),
                            ),
                            Container(
                              padding: EdgeInsets.symmetric(vertical: 50,horizontal: Get.width * 0.15),
                              child: SizedBox(
                                height: 40,
                                child: gradientButton("提 交",circular: 40,onTap: (){
                                  logic.complaint();
                                }),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                barrierColor: Colors.white,
              );
              // complaint/complaint
            }, child: Text("投诉"),),
          ]),
          backgroundColor: Colors.white,
          body: logic.circleDetailData!.postId == null ?SizedBox(): Column(
            children: [
             Expanded(
               child:  ListView(
                 controller: logic.scrollController,
                 padding: EdgeInsets.all(15),
                 shrinkWrap: true,
                 children: [
                   _cardItemUserRow(),

                   InkWidget.title("${logic.circleDetailData!.postTitle}",maxLines: 2),

                   InkWidget.text("${logic.circleDetailData!.postContent}",maxLines: 9999),

                   logic.chewieController == null ?SizedBox():  Container(
                     width: Get.width,
                     height: Get.width / (16/9),
                     child: Chewie(
                       controller: logic.chewieController!,
                     ),
                   ),

                   SizedBox(
                     height: 10,
                   ),

                   player == null ? SizedBox():FijkView(
                     width: Get.width,
                     height: Get.width / (16/9),
                     player: player!,
                     color: Colors.black,
                   ),

                   /// todo：图片
                   Container(
                     width: Get.width,
                     padding: EdgeInsets.symmetric(vertical: 10),
                     child: Wrap(
                       alignment: WrapAlignment.spaceBetween,
                       runSpacing: 10,
                       children: [
                         ...List.generate(logic.circleDetailData!.postUrls!.length, (imgIndex) {
                           return InkWidget.imgNet(logic.circleDetailData!.postUrls![imgIndex],onTap: (){
                             Get.to(()=>ShowImg(imgs: logic.circleDetailData!.postUrls,currentIndex: imgIndex,),preventDuplicates: false);
                           },width: (Get.width - 30 - 20) / 3,height: (Get.width - 30 - 20) / 3,borderRadius: BorderRadius.circular(5));
                         }),
                       ],
                     ),
                   ),

                   SizedBox(
                     height: 10,
                   ),

                   Row(
                     children: [
                       InkWidget.title("评论"),
                       InkWidget.text("(${logic.count}条)",color: Colors.grey,),
                     ],
                   ),

                   ListView(
                     shrinkWrap: true,
                     physics: NeverScrollableScrollPhysics(),
                     children: [
                       ...List.generate(logic.circleDetailData!.comments!.length, (index){
                         return Container(
                           width: Get.width,
                           padding: EdgeInsets.symmetric(vertical: 15),
                           child: Column(
                             crossAxisAlignment: CrossAxisAlignment.start,
                             children: [
                               Row(
                                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                 children: [
                                   Container(
                                     width: 40,
                                     height: 40,
                                     child: ClipOval(
                                       child: CachedNetworkImage(
                                         width: 35,
                                         height: 35,
                                         fit: BoxFit.cover,
                                         imageUrl: "${logic.circleDetailData!.comments![index].commentUserPic}",
                                       ),
                                     ),
                                   ),

                                   Container(
                                     width: Get.width  - 30 - 50,
                                     height: 40,
                                     child: Row(
                                       children: [
                                         Expanded(
                                           child: Text("${logic.circleDetailData!.comments![index].commentUserName}",style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold),maxLines: 2,overflow: TextOverflow.ellipsis,),
                                         ),
                                         Container(
                                           padding: EdgeInsets.only(left: 15,),
                                           width: 85,
                                           height: 30,
                                           child: OutlineButton(
                                             onPressed: (){
                                               logic.hfUser = logic.circleDetailData!.comments![index].commentUserName!;
                                               logic.hfUserId = logic.circleDetailData!.comments![index].commentUserId;
                                               logic.hfId = logic.circleDetailData!.comments![index].commentId;
                                               logic.update();
                                             },
                                             child: InkWidget.text("回复",color: Colors.green),
                                             borderSide: BorderSide(
                                               color: Colors.green,
                                             ),
                                           ),
                                         ),
                                       ],
                                     ),
                                   ),
                                 ],
                               ),

                               SizedBox(height: 15,),

                               Row(
                                 children: [
                                   SizedBox(
                                     width: 50,
                                   ),
                                   Expanded(child: Text("${logic.circleDetailData!.comments![index].commentContent}",style: TextStyle(fontSize: 14,color: Colors.grey.shade800),),),
                                 ],
                               ),

                               SizedBox(height: 5,),

                               Row(
                                 children: [
                                   SizedBox(
                                     width: 50,
                                   ),
                                   InkWidget.text("${logic.circleDetailData!.comments![index].commentTime}",color: Colors.grey,),
                                 ],
                               ),

                               Row(
                                 children: [
                                   SizedBox(
                                     width: 50,
                                   ),
                                   Expanded(child: Divider(color: Colors.grey.shade300,height: 10,)),
                                 ],
                               ),

                               Row(
                                 children: [
                                   SizedBox(
                                     width: 50,
                                   ),
                                   Expanded(
                                     child: ListView(
                                       shrinkWrap: true,
                                       physics: NeverScrollableScrollPhysics(),
                                       children: [
                                         ...List.generate(logic.circleDetailData!.comments![index].replys!.length, (ix){
                                           return Container(
                                             width: Get.width,
                                             padding: EdgeInsets.symmetric(vertical: 15),
                                             child: Column(
                                               crossAxisAlignment: CrossAxisAlignment.start,
                                               children: [
                                                 Row(
                                                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                   children: [
                                                     Container(
                                                       width: 40,
                                                       height: 40,
                                                       child: ClipOval(
                                                         child: CachedNetworkImage(
                                                           width: 35,
                                                           height: 35,
                                                           fit: BoxFit.cover,
                                                           imageUrl: "${logic.circleDetailData!.comments![index].replys![ix].replyUserPic}",
                                                         ),
                                                       ),
                                                     ),

                                                     Container(
                                                       width: Get.width  - 30 - 100,
                                                       height: 40,
                                                       child: Row(
                                                         children: [
                                                           Expanded(
                                                             child: Text("${logic.circleDetailData!.comments![index].replys![ix].replyUserName}",style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold),maxLines: 2,overflow: TextOverflow.ellipsis,),
                                                           ),
                                                         ],
                                                       ),
                                                     ),
                                                   ],
                                                 ),

                                                 SizedBox(height: 15,),

                                                 Row(
                                                   children: [
                                                     SizedBox(
                                                       width: 50,
                                                     ),
                                                     Expanded(child: Text("${logic.circleDetailData!.comments![index].replys![ix].replyContent}",style: TextStyle(fontSize: 14,color: Colors.grey.shade800),),),
                                                   ],
                                                 ),

                                                 SizedBox(height: 5,),

                                                 Row(
                                                   children: [
                                                     SizedBox(
                                                       width: 50,
                                                     ),
                                                     InkWidget.text("${logic.circleDetailData!.comments![index].replys![ix].replyTime}",color: Colors.grey,),
                                                   ],
                                                 ),

                                                 Row(
                                                   children: [
                                                     SizedBox(
                                                       width: 50,
                                                     ),
                                                     Expanded(child: Divider(color: Colors.grey.shade300,height: 10,)),
                                                   ],
                                                 ),
                                               ],
                                             ),
                                           );
                                         }),
                                       ],
                                     ),
                                   ),
                                 ],
                               ),
                             ],
                           ),
                         );
                       }),
                     ],
                   ),
                 ],
               ),
             ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 15),
                width: Get.width,
                height: 60,
                child: Row(
                  children: [
                    Expanded(
                      child: textFieldWidget(
                        onTap: (){
                          print("22222");
                          logic.setListOut();
                        },
                        controller: logic.textEditingController,
                        height: 45,
                        hintText: logic.hfUser==null ?"发表您的评论...":"回复 ${logic.hfUser}",
                      ),
                    ),
                    SizedBox(width: 15,),
                    gradientButton(logic.hfUser==null ?"评论":"回复",onTap: () async{
                      if(logic.hfUser==null){
                        await logic.publishComment(logic.circleDetailData!.postId,logic.circleDetailData!.userId);
                      }else{
                        await logic.publishReply();
                      }
                      logic.setListOut();
                    }),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  /// todo：卡片user行
  _cardItemUserRow() {
    return Container(
      width: Get.width,
      padding: EdgeInsets.only(bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Row(
              children: [
                InkWidget.imgNet(
                  "${logic.circleDetailData!.userPic}",
                  fit: BoxFit.cover,
                  width: 55,
                  height: 55,
                  clip: true,
                ),
                SizedBox(width: 5,),
                Expanded(
                    child: SizedBox(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          InkWidget.title("${logic.circleDetailData!.nikeName}",fonSize: 13,maxLines: 2),
                          InkWidget.text("${logic.circleDetailData!.releaseTime}",fonSize: 11,color: Colors.grey),
                        ],
                      ),
                      height: 55,
                    )
                ),
                SizedBox(width: 5,),
              ],
            ),
          ),

          Offstage(
            offstage: Constant.userDetailData!.id == logic.circleDetailData!.userId,
            child: SizedBox(
              width: 80,
              height: 30,
              child: gradientButton(logic.circleDetailData!.isAttention!?"已关注":"+关注",onTap: () async{

              },bgColor: logic.circleDetailData!.isAttention! ? Colors.grey:null),
            ),
          )
        ],
      ),
    );
  }





  @override
  void dispose() {
    Get.delete<GuangChangDetailsLogic>();
    if(logic.chewieController!=null){
      logic.chewieController!.dispose();
      logic.videoPlayerController1!.dispose();
    }
    super.dispose();
  }
}